package leetcode.D100.T38;

/**
 * @File Info: leetcode -- <Solution>
 * @Author: 18362
 * @Create: 2022-03-11 18:36:17 星期五
 */

class Solution {
    public String countAndSay(int n) {
        if (n <= 1)
            return "1";
        String s = countAndSay(n - 1);
        int i = 0, j = 0;
        StringBuilder sb = new StringBuilder();
        while(i < s.length()) {
            while (j < s.length() && s.charAt(j) == s.charAt(i)) {
                j++;
            }
            sb.append(String.valueOf(j-i));
            sb.append(s.charAt(i));
            i = j;
        }
        return sb.toString();
    }
}